package com.xja.house.ssm.controller;

import com.xja.house.ssm.entity.House;
import com.xja.house.ssm.entity.Users;
import com.xja.house.ssm.service.HouseService;
import com.xja.house.ssm.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("qiantai")
@SessionAttributes("loginuser")
public class QianTaiController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private HouseService houseService;

    @RequestMapping("list")
    public String list(Model model){
        try {
            List<House> houseList = houseService.selectAll();
            model.addAttribute("houseList",houseList);
            return "list";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "error";
    }
    @RequestMapping("detail")
    public String detail(Integer id,Model model){
        try {
            House house = houseService.selectById(id);
            model.addAttribute("house",house);
            return "detail";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "error";
    }

    @RequestMapping("login")
    public String login(Users users, Model model){

        try {
            Users loginuser = usersService.selectByName(users.getName());
            if(loginuser==null){
                model.addAttribute("msg","用户名错误");
                return "login";
            }
            if(!users.getPassword().equals(loginuser.getPassword())){
                model.addAttribute("msg","密码错误");
                return "login";
            }
            model.addAttribute("msg","登陆成功");
            model.addAttribute("loginuser",loginuser);
            if("true".equals(loginuser.getIsadmin())){
                return "redirect:/houtai/guanli.do";
            }
            return "redirect:/qiantai/list.do";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "error";
    }

}
